﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Text;

namespace DBUtil
{
    /// <summary>
    /// 更新项
    /// </summary>
    public class UpdateBatchItem
    {
        private UpdateBatchItem() { }
        /// <summary>
        /// 表名
        /// </summary>
        public string TableName { get; private set; }
        /// <summary>
        /// 以键值对表示的更新列
        /// </summary>
        public IDictionary<string, object> SetDic { get; private set; }
        /// <summary>
        /// 更新时的Where条件，以"and"开头
        /// </summary>
        public string WhereSql { get; private set; }
        /// <summary>
        /// 是否使用用户指定的Where字句
        /// </summary>
        internal bool UseRawSql { set; get; }
        /// <summary>
        /// 更新时用于生成Where子句的字典
        /// </summary>
        public IDictionary<string, object> WhereDic { get; private set; }

        /// <summary>
        /// 创建更新项，使用字典生成Where子句
        /// </summary>
        /// <param name="tableName">表名</param>
        /// <param name="setDic">以键值对表示的更新列</param>
        /// <param name="whereDic">以键值对表示的更新条件</param>
        /// <returns></returns>
        public static UpdateBatchItem Create(string tableName, IDictionary<string, object> setDic, IDictionary<string, object> whereDic)
        {
            return new UpdateBatchItem()
            {
                TableName = tableName,
                SetDic = setDic,
                WhereDic = whereDic,
                UseRawSql = false
            };
        }

        /// <summary>
        /// 创建更新项，手动指定Where子句
        /// </summary>
        /// <param name="tableName">表名</param>
        /// <param name="setDic">以键值对表示的更新列</param>
        /// <param name="whereSql">更新时Where子句，以"and"开头</param>
        /// <param name="parameters">Where子句中的参数</param>
        /// <returns></returns>
        public static UpdateBatchItem Create(string tableName, IDictionary<string, object> setDic, string whereSql, IDictionary<string, object> parameters = null)
        {
            return new UpdateBatchItem()
            {
                TableName = tableName,
                SetDic = setDic,
                WhereSql = whereSql,
                WhereDic = parameters,
                UseRawSql = true
            };
        }
    }
}
